<?php

namespace app\backend\controller;

use app\common\model\GoodsCategory as GoodsCategoryModel;
use think\Controller;
use think\Validate;
use think\Request;
use think\facade\Env;
use app\common\model\Shoptype as stype;
class GoodsCategory extends Controller
{
    public function index(){

        return $this->fetch();
    }
    public  function goods_category(){
        $limit = request()->param('limit');
        $list = GoodsCategoryModel::paginate($limit);
        $count = GoodsCategoryModel::count();
        $data = ['code' => 0, 'msg' => '用户列表', 'count' => $count, 'data' => $list->items()];
        return json($data);

    }
    //切换分类显示状态
    public function change_show(Request $request)
    {
        if($request->isPost()) {
            $id = $request->param('id');
            $res = GoodsCategoryModel::where(['cat_id'=>$id])->find();
            if($res['is_show'] == 1){
                $update = GoodsCategoryModel::where('cat_id', $id)->update(['is_show'=>0]);
                if($update){
                    $data['status'] = 1;
                    $data['msg'] = "更改成功";
                }else{
                    $data['status'] = 2;
                    $data['msg'] = "更改失败";
                }
            }else{
                $update = GoodsCategoryModel::where('cat_id', $id)->update(['is_show'=>1]);
                if($update){
                    $data['status'] = 1;
                    $data['msg'] = "更改成功";
                }else{
                    $data['status'] = 2;
                    $data['msg'] = "更改失败";
                }
            }

            return $data;
        }
    }
    //删除分类
    public function del_cat(Request $request)
    {
        if($request->isPost()) {
            $id = $request->param('id');
            $res = GoodsCategoryModel::where(['cat_id'=>$id])->find();
            if(GoodsCategoryModel::where(['cat_id'=>$id])->delete()){
                $data['status'] = 1;
                $data['msg'] = lang("删除成功");
            }else{
                $data['status'] = 2;
                $data['msg'] = lang("删除失败");
            }
            return $data;
        }
    }
    //修改分类排序值
    public function change_sort(Request $request)
    {
        if($request->isPost()) {
            $id = $request->param('id');
            $sort = $request->param('sort');
            $res = GoodsCategoryModel::where(['cat_id'=>$id])->find();
            if($res['sort_order'] == $sort){
                $data['status'] = 3;
                return $data;
            }
            $update = GoodsCategoryModel::where('cat_id', $id)->update(['sort_order'=>$sort]);
            if($update){
                $data['status'] = 1;
                $data['msg'] = "更改成功";
            }else{
                $data['status'] = 2;
                $data['msg'] = "更改失败";
            }
            return $data;
        }
    }
    //修改分类
    public  function edit_cat(Request $request){

        if($request->isPost()) {
            $data = $_POST;
            //print_r($data);exit;
            $validate = new \app\common\validate\GoodsCategory;
            if (!$validate->check($data)) {
                $res['status'] = 2;
                $res['msg'] = $validate->getError();
            }else{
                $img_obj=request()->file();
                if(isset($img_obj['titleFile'])){
                    $file=$img_obj['titleFile'];
                    $img = $file->validate(['size' => 2048000, 'ext' => 'jpg,png,gif'])->move('uploads');
                    if ($img) {
                        // 成功上传后 获取上传信息
                        // 输出 42a79759f284b767dfcb2a0197904287.jpg
                        $img_name = $img->getSaveName();
                        $data['thumb']="/uploads/$img_name";
                    } else {
                        // 上传失败获取错误信息
                        $img_name = $file->getError();
                    }
                }
                $update = GoodsCategoryModel::where('cat_id', $data['cat_id'])->update($data);
                if($update){
                    $res['status'] = 1;
                    $res['msg'] = '修改成功';
                }else{
                    $res['status'] = 2;
                    $res['msg'] = '修改失败';
                }
            }
            return $res;

        }else{
            $id = $request->param('id');
            $res = GoodsCategoryModel::where(['cat_id'=>$id])->find();
            $this->assign('res',$res);
            return $this->fetch();
        }
    }
    //添加分类
    public  function  add_cat(Request $request){
        if($request->isPost()) {
             //print_r($_POST);exit;
            $data = $_POST;
            $validate = new \app\common\validate\GoodsCategory;

            if (!$validate->check($data)) {
                $res['status'] = 2;
                $res['msg'] = $validate->getError();
            }else{
                $img_obj=request()->file();
                if(isset($img_obj['titleFile'])){
                    $file=$img_obj['titleFile'];
                    $img = $file->validate(['size' => 2048000, 'ext' => 'jpg,png,gif'])->move('uploads');
                    if ($img) {
                        // 成功上传后 获取上传信息
                        // 输出 42a79759f284b767dfcb2a0197904287.jpg
                        $img_name = $img->getSaveName();
                        $data['thumb']="/uploads/$img_name";
                    } else {
                        // 上传失败获取错误信息
                        $img_name = $file->getError();
                    }
                }
                $res = GoodsCategoryModel::create($data);
                if($res){
                    $res['status'] = 1;
                    $res['msg'] = '提交成功';
                }else{
                    $res['status'] = 2;
                    $res['msg'] = '提交异常';
                }
            }
            return $res;

        }
        return $this->fetch();


    }
    public function good()
    {
        $selectOption = GoodsCategoryModel::where('cat_id','<>',0)->select()->toArray();
        $shoptype_list=stype::all();
        $this->assign('shoptype_list',$shoptype_list);
        $this->assign('selectOption',$selectOption);
        return $this->fetch();
    }
    /**
     * 上传图片
     */
    public function uploads()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('cat_thumb');
        //var_dump($file);exit;

        if($file){
            // foreach($files as $file){
            // 移动到根目录/public/uploads/ 目录下
            $info = $file->validate(['ext'=>'jpg,png,gif'])->move(Env::get('root_path') . 'public/uploads');
            if($info){
                // 成功上传后
                $str = str_replace('\\','/',$info->getSaveName());
                $data['img'] = '/uploads/'.$str;


            }else{
                $data['img'] == '';
            }
            return $data['img'];
            // }
        }
    }

}




